package cn.stylefeng.guns.modular.api;

import cn.stylefeng.guns.base.consts.ConstantsContext;
import cn.stylefeng.guns.modular.business.service.AccountService;
import cn.stylefeng.guns.sms.core.enums.SmsTemplateCode;
import cn.stylefeng.guns.sys.core.util.StringUtils;
import cn.stylefeng.guns.sys.modular.system.model.UserDto;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 用户api
 *
 * @author jwj
 * @Date 2019-07-12 14:20:25
 */
@RestController
@RequestMapping("/gunsApi/account")
public class AccountApi extends BaseController {

    @Autowired
    private AccountService accountService;


    @PostMapping("/reg")
    public ResponseData reg(UserDto userDto) {
        if (StringUtils.isAnyEmpty(userDto.getAccount(), userDto.getPassword())) {
            return ResponseData.error(500, "账户和密码不能为空");
        }
        if (StringUtils.isEmpty(userDto.getSmsCode())) {
            return ResponseData.error(500, "短信验证码不能为空");
        }
        accountService.reg(userDto);
        return ResponseData.success();
    }

    @ApiOperation("获取个人信息")
    @GetMapping("/information")
    public ResponseData information(@RequestParam long userId) {
        return ResponseData.success(accountService.information(userId));
    }

    @ApiOperation("修改用户昵称")
    @PostMapping("/updateName")
    public ResponseData updateName(@RequestParam long userId, @RequestParam String name) {
        if (accountService.updateName(userId, name)) {
            return ResponseData.success();
        }
        return ResponseData.error("修改用户昵称失败");
    }

    @ApiOperation("接收微信消息推送")
    @PostMapping("/messagePush")
    public ResponseData messagePush(@RequestParam long userId, @RequestParam String messagePush) {
        if (accountService.messagePush(userId, messagePush)){
            return ResponseData.success();
        }
        return ResponseData.error("更新微信消息推送失败");
    }

    @ApiOperation("更换手机号")
    @PostMapping("changePhone")
    public ResponseData changePhone(@RequestParam long userId, @RequestParam String oldPhone, @RequestParam String newPhone, @RequestParam String smsCode) {
        if (StringUtils.isAnyBlank(oldPhone, newPhone)) {
            return ResponseData.error(500, "手机号不能为空");
        }
        if (StringUtils.isEmpty(smsCode)) {
            return ResponseData.error(500, "短信验证码不能为空");
        }
        if (accountService.changePhone(userId, oldPhone, newPhone, smsCode)){
            return ResponseData.success();
        }
        return ResponseData.error("更换手机号失败");
    }

    @ApiOperation("修改密码")
    @PostMapping("changePassword")
    public ResponseData changePassword(@RequestParam long userId, @RequestParam String oldPassword, @RequestParam String newPassword, @RequestParam String repeatPassword) {
        if (StringUtils.isAnyBlank(oldPassword, newPassword, repeatPassword)) {
            return ResponseData.error(500, "密码不能为空");
        }
        if (!newPassword.equals(repeatPassword)) {
            return ResponseData.error(405,"两次输入密码不一致");
        }
        if (accountService.changePassword(userId, oldPassword, newPassword)){
            return ResponseData.success();
        }
        return ResponseData.error("修改密码失败");
    }

    @ApiOperation("联系我们")
    @GetMapping("contactUs")
    public ResponseData contactUs(){
        return ResponseData.success(accountService.contactUs());
    }

    @ApiOperation("发送短信")
    @GetMapping("/sendSMS")
    public ResponseData sendSMS(@RequestParam String phone, @RequestParam Integer type) {
        if (StringUtils.isEmpty(phone)) {
            return ResponseData.error("手机号不能为空");
        }
        if (type != null) {
            type = SmsTemplateCode.CHANGE.getCode();
        }
        String temCodeKey = SmsTemplateCode.getDescByCode(type);
        if (StringUtils.isEmpty(temCodeKey)) {
            return ResponseData.error("手机配置信息错误");
        }
        String temCode = (String) ConstantsContext.getConstntsMap().get(temCodeKey);
        boolean result = accountService.sendSMS(phone, temCode);
        if (result) {
            return ResponseData.success("成功");
        } else {
            return ResponseData.error("发送短信错误");
        }
    }

    @ApiOperation("验证短信")
    @GetMapping("/verify-code")
    public ResponseData verifySmsCode(@RequestParam String phone, @RequestParam String code) {
        if (StringUtils.isEmpty(phone)) {
            return ResponseData.error("手机号不能为空");
        }
        if (StringUtils.isEmpty(code)) {
            return ResponseData.error("验证码不能为空");
        }

        boolean result = accountService.invalidSMS(phone, code);
        if (result) {
            return ResponseData.success("成功");
        } else {
            return ResponseData.error("验证码不正确");
        }
    }

}


